package com.tanhua.manager.listener;

import com.tanhua.manager.domain.Log;
import com.tanhua.manager.service.LogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@RocketMQMessageListener(consumerGroup = "tanhuaLogGroup",topic = "tanhua-logs")
@Component // 这个注解不要忘记了
public class LogMessageListener implements RocketMQListener<Map> {

    @Autowired
    private LogService logService;


    @Override
    public void onMessage(Map mqMap) {

//        这里有坑，放进去的是一个Long，但是这里取出来的是Interger
       Long userId = ((Integer)mqMap.get("userId")).longValue();
      String type = (String) mqMap.get("type"); //0101为登录，0102为注册
      String logTime = (String) mqMap.get("logTime");

        Log log = new Log();
        log.setUserId(userId);
        log.setType(type);
        log.setLogTime(logTime);
        log.setPlace("默认地址");
        log.setEquipment("默认的设备");
        logService.save(log);
        System.out.println("消费者已经消费消息了................");
    }
}
